package com.maple.easyui;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.maple.servlet.ServletUtilsw;

public class EasyuiUtilsw {
	/**
	 * easyui 
	 * @param <T>
	 * @param total
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> String gainGridJsonStr(int total,List<T> list)
	{
		Map map=new HashMap();
		map.put("total", total);
		map.put("rows", list);
		String gridJson=JSONObject.fromObject(map).toString();
		return gridJson;
	}
	/**
	 * easyui 
	 */
	@SuppressWarnings("unchecked")
	public <T> void writeGridJsonStr(int total,List<T> list)
	{
		String gridJson=gainGridJsonStr(total, list);
		try {
			HttpServletResponse response= ServletUtilsw.gainResponse();
//			response.setContentType("text/javascript charset=gbk");
			PrintWriter writer =response.getWriter();
			writer.write(gridJson);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public <T> String gainJsonStr(T t)
	{
		String json=JSONObject.fromObject(t).toString();
		return json;
	}
	@SuppressWarnings("unchecked")
	public <T> void writeJsonStr(T t)
	{
		String json=gainJsonStr(t);
		try {
			HttpServletResponse response= ServletUtilsw.gainResponse();
			PrintWriter writer =response.getWriter();
			writer.write(json);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
}
